package com.itgungnir.diyshuo.activity.listview_activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.base.BaseActivity;
import com.itgungnir.diyshuo.share.SharedURL;

import java.util.HashMap;
import java.util.Map;

/**
 * 界面中只显示一个ListView的Activity
 */
public class ListViewActivity extends BaseActivity implements ListViewContract.View {
    private TextView title;
    private Button back;
    private PullToRefreshListView lv;

    private ListViewPresenter presenter = new ListViewPresenter(this);
    private String recommendId;
    private boolean isAdd;
    private int pageId = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title = (TextView) findViewById(R.id.find_listview_tv_title);
        back = (Button) findViewById(R.id.find_listview_btn_back);
        lv = (PullToRefreshListView) findViewById(R.id.find_listview_rlv_lv);
        lv.setMode(PullToRefreshBase.Mode.BOTH);
        initViews();
        initEvents();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_listview;
    }

    /**
     * 初始化布局界面
     */
    private void initViews() {
        Intent intent = getIntent();
        recommendId = intent.getStringExtra("id");
        isAdd = intent.getBooleanExtra("isAdd", false);
        // 初始化标题中的文本
        setTextToTitle(intent.getStringExtra("title"));
        // 初始化PullToRefreshListView中的数据
        setContentToListView();
    }

    /**
     * 初始化控件的交互事件
     */
    private void initEvents() {
        // 当点击返回按钮时触发的事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackClickListener();
            }
        });
        // 当点击PullToRefreshListView中的某一项时触发的事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onListViewItemClickListener();
            }
        });
        // 当PullToRefreshListView上拉或下拉时触发的事件
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 下拉刷新
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 上拉加载
            }
        });
    }

    @Override
    public void setTextToTitle(String text) {
        presenter.setTextToTitle(title, text);
    }

    @Override
    public void setContentToListView() {
        presenter.setContentToListView(ListViewActivity.this, lv, getParameterMap(pageId, isAdd, recommendId));
    }

    @Override
    public void onBackClickListener() {
        presenter.onBackClickListener(ListViewActivity.this);
    }

    @Override
    public void onListViewItemClickListener() {
        presenter.onListViewItemClickListener();
    }

    @Override
    public void onListViewPullDownListener() {
    }

    @Override
    public void onListViewPullUpListener() {
    }

    /**
     * 根据参数类型编辑参数列表
     */
    private Map<String, String> getParameterMap(int pageId, boolean isAdd, String recommend) {
        Map<String, String> params = new HashMap<>();
        // mobile=no&version=4&module=forumdisplay&submodule=checkpost&fid=2&page=1&tpp=10&orderby=dateline&recommend_add=1
        params.put(SharedURL.MOBILE_NAME, SharedURL.MOBILE_VALUE);
        params.put(SharedURL.VERSION_NAME, SharedURL.VERSION_VALUE);
        params.put(SharedURL.MODULE_NAME, SharedURL.MODULE_VALUE_FORUM);
        params.put(SharedURL.SUBMODULE_NAME, SharedURL.SUBMODULE_VALUE);
        params.put(SharedURL.FID_NAME, SharedURL.FID_VALUE);
        params.put(SharedURL.PAGE_NAME, pageId + "");
        params.put(SharedURL.TPP_NAME, SharedURL.TPP_VALUE);
        params.put(SharedURL.ORDERBY_NAME, SharedURL.ORDERBY_VALUE);
        if (isAdd) {
            params.put(SharedURL.RECOMMEND_ADD_NAME, recommend);
        } else {
            params.put(SharedURL.RECOMMENDS_NAME, recommend);
        }
        return params;
    }
}